/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mx.im.sinocab.dao;

import com.mx.im.sinocab.bean.Employ;
import com.mx.im.sinocab.bean.Menu;
import com.mx.im.sinocab.bean.Option;
import com.mx.im.sinocab.bean.User;
import com.mx.im.sinocab.bean.Usertype;
import com.mx.im.sinocab.util.Conection;
import com.mx.im.sinocab.util.Constant;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author javilesc
 */
public class AdminMenuDAO implements Constant {
    //private Connection conexion;

    private PreparedStatement stat;

    public ArrayList<Option> listMenuOptions() {
        Conection cone = new Conection();
        //conexion = cone.getConection();
        ArrayList<Option> optionsList = new ArrayList<Option>();

        try {
            //conexion.setAutoCommit(false);
            stat = cone.getConection().prepareStatement(SQL_LIST_MENU_OPTIONS);

            ResultSet result = stat.executeQuery();

            //conexion.commit();
            //conexion.setAutoCommit(true);
            Option optionFind;
            while (result.next()) {

                optionFind =
                        new Option(
                        Integer.valueOf(result.getObject("idOption") != null ? result.getObject("idOption").toString() : "0"),
                        new User(
                        Integer.valueOf(result.getObject("idUser") != null ? result.getObject("idUser").toString() : "0"),
                        new Employ(
                        Integer.valueOf(result.getObject("idEmploy") != null ? result.getObject("idEmploy").toString() : "0"),
                        result.getObject("name") != null ? result.getObject("name").toString() : "",
                        result.getObject("lastName") != null ? result.getObject("lastName").toString() : "",
                        result.getObject("address") != null ? result.getObject("address").toString() : "",
                        result.getObject("phone") != null ? result.getObject("phone").toString() : "",
                        result.getObject("email") != null ? result.getObject("email").toString() : "",
                        result.getDate("em_birthday") != null ? result.getDate("em_birthday") : new Date(), "gender"),
                        new Usertype(
                        Integer.valueOf(result.getObject("idUserType") != null ? result.getObject("idUserType").toString() : "0"),
                        result.getObject("ut_definition") != null ? result.getObject("ut_definition").toString() : "",
                        result.getObject("ut_description") != null ? result.getObject("ut_description").toString() : ""),
                        result.getObject("user") != null ? result.getObject("user").toString() : "",
                        null/*Password*/,
                        null/*DateExpiry*/),
                        new Menu(
                        Integer.valueOf(result.getObject("m_idmenu") != null ? result.getObject("m_idmenu").toString() : "0"),
                        new User(
                        Integer.valueOf(result.getObject("um_idUser") != null ? result.getObject("um_idUser").toString() : "0"),
                        new Employ(
                        Integer.valueOf(result.getObject("em_idEmploy") != null ? result.getObject("em_idEmploy").toString() : "0"),
                        result.getObject("em_name") != null ? result.getObject("em_name").toString() : "",
                        result.getObject("em_lastName") != null ? result.getObject("em_lastName").toString() : "",
                        result.getObject("em_address") != null ? result.getObject("em_address").toString() : "",
                        result.getObject("em_phone") != null ? result.getObject("em_phone").toString() : "",
                        result.getObject("em_email") != null ? result.getObject("em_email").toString() : "",
                        result.getDate("em_birthday") != null ? result.getDate("em_birthday") : new Date(), "em_gender"),
                        new Usertype(
                        Integer.valueOf(result.getObject("utm_idUserType") != null ? result.getObject("utm_idUserType").toString() : "0"),
                        result.getObject("utm_definition") != null ? result.getObject("utm_definition").toString() : "",
                        result.getObject("utm_description") != null ? result.getObject("utm_description").toString() : ""),
                        result.getObject("um_user") != null ? result.getObject("um_user").toString() : "",
                        null/*Password*/,
                        null/*DateExpiry*/),
                        result.getObject("m_menu") != null ? result.getObject("m_menu").toString() : "",
                        result.getObject("m_description") != null ? result.getObject("m_description").toString() : "",
                        Boolean.parseBoolean(result.getObject("m_status") != null ? result.getObject("m_status").toString() : "false")),
                        result.getObject("option") != null ? result.getObject("option").toString() : "",
                        result.getObject("description") != null ? result.getObject("description").toString() : "",
                        result.getObject("actionRequest") != null ? result.getObject("actionRequest").toString() : "",
                        Boolean.parseBoolean(result.getObject("status") != null ? result.getObject("status").toString() : "false"));

                optionsList.add(optionFind);
            }

            if (stat != null) {
                stat.close();
            }

        } catch (Exception e) {
        } finally {
            cone.closeConection();
            return optionsList;
        }
    }

    public boolean insertOption(Option option) throws SQLException {
        boolean result = false;
        Conection cone = new Conection();
        //conexion = cone.getConection();
        if (cone != null) {

            //conexion.setAutoCommit(false);
            this.stat = cone.getConection().prepareStatement(SQL_INSERT_OPTION);

            this.stat.setInt(1, option.getUser().getIdUser());
            this.stat.setInt(2, option.getMenu().getIdMenu());
            this.stat.setString(3, option.getOption());
            this.stat.setString(4, option.getDescription());
            this.stat.setString(5, option.getActionRequest());
            System.out.println("Estado: " + option.isStatus());
            result = this.stat.executeUpdate() == 1 ? true : false;

            if (this.stat != null) {
                this.stat.close();
            }

            cone.closeConection();

        }
        return result;

    }

    public boolean insertMenu(Menu menu) throws SQLException {
        boolean result = false;
        Conection cone = new Conection();
        //conexion = cone.getConection();
        if (cone != null) {

            //conexion.setAutoCommit(false);
            this.stat = cone.getConection().prepareStatement(SQL_INSERT_MENU);

            this.stat.setInt(1, menu.getUser().getIdUser());
            this.stat.setString(2, menu.getMenu());
            result = Boolean.valueOf(String.valueOf(this.stat.executeUpdate()));

            if (this.stat != null) {
                this.stat.close();
            }

            cone.closeConection();

        }
        return result;

    }
}
